# Build recipe for ruby.
#
# Copyright (c) 2019 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

program=ruby
pkgname=ruby2
version=2.6.5
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/ruby"

tarname=${program}-${version}.tar.bz2

# Remote source(s)
fetch="http://ftp.ruby-lang.org/pub/ruby/${version%.*}/$tarname"

description="
Ruby programming language.

Ruby is a dynamic, open source programming language with a focus
on simplicity and productivity.  It has an elegant syntax that is
natural to read and easy to write.
"

homepage=http://www.ruby-lang.org
license="GPLv2+ | BSD + Custom declared files (see LEGAL)"

# Source documentation
docs="
  BSDL CONTRIBUTING.md COPYING* ChangeLog GPL KNOWNBUGS.rb
  LEGAL NEWS README*
"
docsdir="${docdir}/${pkgname}-${version}"

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # A patch to avoid installing platform-specific gems with binaries
    # linked against Glibc (Thanks to "Alpine Linux")
    patch -Np1 -i "${worktree}/patches/ruby/rubygems-avoid-platform-specific-gems.patch"

    patch -Np1 -i "${worktree}/patches/ruby/fix-get_main_stack.patch"
    patch -Np1 -i "${worktree}/patches/ruby/test_insns-lower-recursion-depth.patch"
    patch -Np1 -i "${worktree}/patches/ruby/avoid-rdoc-hook-when-its-failed-to-load-rdoc-library.patch"

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" tool/config.guess
    cp -f "${worktree}/archive/common/config.sub" tool/config.sub

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    ./configure \
    CPPFLAGS="-D_GNU_SOURCE -fno-omit-frame-pointer -fno-strict-aliasing" \
    CFLAGS="$QICFLAGS -fno-omit-frame-pointer -fno-strict-aliasing" \
    CXXFLAGS="$QICXXFLAGS -fno-omit-frame-pointer -fno-strict-aliasing" \
    LDFLAGS="$QILDFLAGS" INSTALL="install" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-shared \
     --enable-ipv6 \
     --enable-load-relative \
     --with-sitedir=/usr/local/share/ruby \
     --with-sitearchdir=/usr/local/lib${libSuffix}/ruby \
     --without-compress-debug-sections \
     --disable-rpath \
     --disable-install-doc \
     --disable-install-rdoc \
     --disable-install-capi \
     --build="$(cc -dumpmachine)" \
     ac_cv_func_isnan=yes \
     ac_cv_func_isinf=yes

    make -j${jobs} V=1 optflags="-O2"
    make -j${jobs} DESTDIR="$destdir" install

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 '{}' +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"/
}

